package com.gp.common.utils;

import java.lang.reflect.Method;

import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;

/**
 * @author: 弓成龙
 * @date : 2018年2月25日
 * @email : gcx909109@sina.cn
 * @description:

@Configuration
@EnableCaching
 */
public class RedisCacheConfig extends CachingConfigurerSupport {
  private volatile JedisConnectionFactory jedisConnectionFactory;
  private volatile RedisTemplate<String, String> redisTemplate;
  private volatile RedisCacheManager redisCacheManager;

  public RedisCacheConfig() {
    super();
  }

  /**
   * 带参数的构造方法 初始化所有的成员变量
   * 
   * @param jedisConnectionFactory
   * @param redisTemplate
   * @param redisCacheManager
   */
  public RedisCacheConfig(JedisConnectionFactory jedisConnectionFactory,
      RedisTemplate<String, String> redisTemplate, RedisCacheManager redisCacheManager) {
    this.jedisConnectionFactory = jedisConnectionFactory;
    this.redisTemplate = redisTemplate;
    this.redisCacheManager = redisCacheManager;
  }

  public JedisConnectionFactory getJedisConnecionFactory() {
    return jedisConnectionFactory;
  }

  public RedisTemplate<String, String> getRedisTemplate() {
    return redisTemplate;
  }

  public RedisCacheManager getRedisCacheManager() {
    return redisCacheManager;
  }
  // @Bean
  // public JedisConnectionFactory redisConnectionFactory() {
  // JedisConnectionFactory redisConnectionFactory = new JedisConnectionFactory();
  //
  // // Defaults
  // redisConnectionFactory.setHostName("192.168.132.140");
  // redisConnectionFactory.setPort(6379);
  // return redisConnectionFactory;
  // }
  //
  // @Bean
  // public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory cf) {
  // RedisTemplate<String, String> redisTemplate = new RedisTemplate<String, String>();
  // redisTemplate.setConnectionFactory(cf);
  // return redisTemplate;
  // }
  //
  // @Bean
  // public CacheManager cacheManager(RedisTemplate redisTemplate) {
  // RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate);
  //
  // // Number of seconds before expiration. Defaults to unlimited (0)
  // cacheManager.setDefaultExpiration(3000); // Sets the default expire time (in seconds)
  // return cacheManager;
  // }

  @Bean
  public KeyGenerator customKeyGenerator() {
    return new KeyGenerator() {
      @Override
      public Object generate(Object o, Method method, Object... objects) {
        StringBuilder sb = new StringBuilder();
        sb.append(o.getClass().getName());
        sb.append(method.getName());
        for (Object obj : objects) {
          sb.append(obj.toString());
        }
        return sb.toString();
      }
    };
  }
}

